import config from '../config/dev.js';
let request = {};

request.rq = (url, data, method) => {
    let headers = {
        'Content-Type': 'application/json;charset=utf-8',
        'lang': 'zh-CN',
        'accept-language': 'zh-CN,zh;q=0.9'
    };
    let params = data || {};
    return new Promise((resolve, reject) => {
        uni.request({
            sslVerify: false,
            url: url,
            method: method,
            data: params,
            header: headers,
            success: async (resp) => {
                setTimeout(() => {
                uni.hideLoading()
                }, 1500);
                let resultInfo = resp.data;                
                resolve(resultInfo);
            },
            fail: (resq) => {
                uni.hideLoading();
                uni.getNetworkType({
                    success: (res) => {
                        if (res.networkType === 'unknown') {
                            uni.showToast({
                                icon: 'none',
                                title: '网络错误，请检查',
                            });
                        }
                    },
                });
                reject(resq.data);
            },
        });
    });
};

request.get = (url, data, type) => {
    if (type == 'dev') {
        return request.rq(config.totalHost + url, data, 'GET');
    }
};

request.post = (url, data, type) => {
    if (type == 'dev') {
        return request.rq(config.totalHost + url, data, 'POST');
    }
};

request.put = (url, data, type) => {
    if (type == 'dev') {
        return request.rq(config.totalHost + url, data, 'PUT');
    }
};

export default request;